package com.whj.servicedriveruser.controller;

import com.whj.internalcommon.constant.DriverCarConstants;
import com.whj.internalcommon.dto.DriverUser;
import com.whj.internalcommon.dto.ResponseResult;
import com.whj.internalcommon.response.DriverUserExistsResponse;
import com.whj.internalcommon.response.OrderDriverResponse;
import com.whj.servicedriveruser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: wanghaijun
 * @Date: 2023/1/11 - 01 - 11 - 16:14
 * @Description: com.whj.servicedriveruser.controller
 */

@RestController
public class UserController {

    @Autowired
    private DriverUserService driverUserService;

    /**
     * 添加司机
     *
     * @param driverUser
     * @return
     */
    @PostMapping("/user")
    public ResponseResult addDriverUser(@RequestBody DriverUser driverUser) {

        return driverUserService.addDriverUser(driverUser);

    }

    /**
     * 更新司机
     *
     * @param driverUser
     * @return
     */
    @PutMapping("/user")
    public ResponseResult updateDriverUser(@RequestBody DriverUser driverUser) {

        return driverUserService.updateDriverUser(driverUser);

    }

    /**
     * 查询司机
     * 如果需要按照司机的多个条件查询此次用 /user
     * @return
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistsResponse> getDriverUser(@PathVariable("driverPhone") String driverPhone) {
        ResponseResult<DriverUser> driverUserByPhone = driverUserService.getDriverUserByPhone(driverPhone);
        DriverUser driverUserDb = driverUserByPhone.getData();
        DriverUserExistsResponse response = new DriverUserExistsResponse();
        int ifExists= DriverCarConstants.DRIVER_EXISTS;
        if(driverUserDb==null){
            ifExists=DriverCarConstants.DRIVER_NOT_EXISTS;
            response.setDriverPhone(driverPhone);
            response.setIfExists(ifExists);
        }else {

            response.setDriverPhone(driverUserDb.getDriverPhone());
            response.setIfExists(ifExists);
        }
        return ResponseResult.success(response);
    }

    /**
     * 根据车辆ID查询订单所需要的信息
     *
     * @param carId
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId") Long carId){
        return driverUserService.getAvailableDriver(carId);
    }
}
